﻿package org.xhs.report.doc
{
    import baidu.ui.managers.*;
    import baidu.wenku.common.*;
    import baidu.wenku.common.model.*;
    import baidu.wenku.common.ui.toolbar.*;
    import baidu.wenku.common.utils.*;
    import baidu.wenku.doc.ui.*;
    import flash.display.*;
    import flash.events.*;

    public class DocMain extends Sprite
    {
        protected var _bg:Sprite;
        protected var _loading:Loading;
        protected var toolBar:ToolBar;
        protected var viewPort:ViewPort;
        protected var loaderInfoVo:LoaderInfoVO;
        protected var screenModel:ScreenModel;

        public function DocMain()
        {
            WenkuLog.addLog(WenkuLog.LOGURL_BEFORE_INIT);
            this.addEventListener(Event.ENTER_FRAME, this.checkLoaded);
            return;
        }// end function

        protected function checkLoaded(event:Event = null) : void
        {
            if (stage && stage.stageWidth > 0 && stage.stageHeight > 0)
            {
                this.removeEventListener(Event.ENTER_FRAME, this.checkLoaded);
                this.init();
            }
            return;
        }// end function

        protected function init() : void
        {
            this.loaderInfoVo = InstanceManager.createSingletonInstance(LoaderInfoVO);
            this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
            this.screenModel.addEventListener(Event.RESIZE, this.stageResize);
            this.reset();
            return;
        }// end function

        protected function reset() : void
        {
            this.clearInterface();
            this.addBG();
            this.addToolBar();
            this.addDocViewer();
            return;
        }// end function

        protected function stageResize(event:Event = null) : void
        {
            this.addBG();
            if (this._loading)
            {
                addChild(this._loading);
                this._loading.x = stage.stageWidth / 2;
                this._loading.y = stage.stageHeight / 2;
            }
            return;
        }// end function

        protected function clearInterface() : void
        {
            while (numChildren)
            {
                
                removeChildAt(0);
            }
            return;
        }// end function

        protected function showLoading(event:Event = null) : void
        {
            if (!this._loading)
            {
                this._loading = new Loading();
            }
            addChild(this._loading);
            this._loading.x = stage.stageWidth / 2;
            this._loading.y = stage.stageHeight / 2;
            this._loading.loadBar.scaleX = this._loading.loadBar.scaleX + (1 - this._loading.loadBar.scaleX) * 0.005;
            this._loading.loadBar.scaleX = Math.min(this._loading.loadBar.scaleX, 0.95);
            return;
        }// end function

        protected function hideLoading(event:Event = null) : void
        {
            if (this._loading)
            {
                if (contains(this._loading))
                {
                    removeChild(this._loading);
                }
                this._loading = null;
            }
            return;
        }// end function

        protected function addBG() : void
        {
            if (!this._bg)
            {
                this._bg = new Sprite();
            }
            var _loc_1:* = this._bg.graphics;
            _loc_1.clear();
            _loc_1.lineStyle(1, Config.BORDER_COLOR);
            _loc_1.beginFill(Config.BG_COLOR);
            _loc_1.drawRect(0, 0, (stage.stageWidth - 1), (stage.stageHeight - 1));
            _loc_1.endFill();
            this._bg.x = 0.5;
            this._bg.y = 0.5;
            addChildAt(this._bg, 0);
            return;
        }// end function

        protected function addToolBar() : void
        {
            if (!this.toolBar)
            {
                this.toolBar = new ToolBar();
            }
            this.toolBar.x = 0;
            this.toolBar.y = 0;
            addChild(this.toolBar);
            this.toolBar.addEventListener(ToolbarEvent.SCREEN_FULL, this.toolBarFullBtnClick);
            this.toolBar.addEventListener(ToolbarEvent.SCREEN_NORMAL, this.toolBarNormalBtnClick);
            this.toolBar.addEventListener(ToolbarEvent.MODE_SINGLE, this.toolBarSingleBtnClick);
            this.toolBar.addEventListener(ToolbarEvent.MODE_DOUBLE, this.toolBarDoubleBtnClick);
            this.toolBar.addEventListener(ToolbarEvent.SEARCH_START, this.toolBarSearchBtnClick);
            this.toolBar.addEventListener(ToolbarEvent.SEARCH_AUTO, this.autoSearch);
            this.toolBar.addEventListener(ToolbarEvent.SEARCH_PREV, this.toolBarSearchPrev3BtnClick);
            this.toolBar.addEventListener(ToolbarEvent.SEARCH_NEXT, this.toolBarSearchNext3BtnClick);
            this.toolBar.addEventListener(ToolbarEvent.SEARCH_CLOSE, this.toolBarSearchCloseBtnClick);
            this.toolBar.addEventListener(ToolbarEvent.PAGETH_CHANGED, this.toolBarPagethChanged);
            this.toolBar.init();
            return;
        }// end function

        protected function addDocViewer() : void
        {
            if (!this.viewPort)
            {
                this.viewPort = new ViewPort();
            }
            this.viewPort.x = 1;
            this.viewPort.y = Config.TB_HEIGHT;
            addChildAt(this.viewPort, getChildIndex(this.toolBar));
            this.viewPort.addEventListener(LocatEvent.SHOW_LOADING, this.showLoading);
            this.viewPort.addEventListener(LocatEvent.STOP_LOADING, this.hideLoading);
            this.viewPort.addEventListener(LocatEvent.SEARCH_RESULT, this.docViewSearchResult);
            this.viewPort.init();
            return;
        }// end function

        protected function toolBarFullBtnClick(event:Event = null) : void
        {
            this.screenModel.stage.displayState = StageDisplayState.FULL_SCREEN;
            return;
        }// end function

        protected function toolBarNormalBtnClick(event:Event = null) : void
        {
            this.screenModel.stage.displayState = StageDisplayState.NORMAL;
            return;
        }// end function

        protected function toolBarSingleBtnClick(event:Event = null) : void
        {
            this.viewPort.switchPagesMode(DocConfig.PAGE_MODE_SINGLE);
            this.toolBar.resetPageModeBtns();
            return;
        }// end function

        protected function toolBarDoubleBtnClick(event:Event = null) : void
        {
            this.viewPort.switchPagesMode(DocConfig.PAGE_MODE_DOUBLE);
            this.toolBar.resetPageModeBtns();
            return;
        }// end function

        protected function toolBarSearchBtnClick(event:Event = null) : void
        {
            var _loc_2:* = this.toolBar.searchKeyWord;
            if (_loc_2 && _loc_2 != "")
            {
                this.viewPort.doSearch(this.viewPort.pageth, _loc_2);
            }
            return;
        }// end function

        protected function autoSearch(event:Event = null) : void
        {
            var _loc_2:* = this.toolBar.searchKeyWord;
            var _loc_3:* = this.toolBar.autoSearchIndex;
            if (_loc_2 && _loc_2 != "" && _loc_3 > -1 && _loc_3 < this.viewPort.pagesAllAvailable)
            {
                this.viewPort.doSearch(_loc_3, _loc_2);
            }
            return;
        }// end function

        protected function toolBarSearchPrev3BtnClick(event:Event = null) : void
        {
            var _loc_2:* = this.toolBar.searchKeyWord;
            var _loc_3:* = Math.max(this.viewPort.pageth, 0);
            if (_loc_2 && _loc_2 != "")
            {
                this.viewPort.doSearch(_loc_3, _loc_2);
            }
            return;
        }// end function

        protected function toolBarSearchNext3BtnClick(event:Event = null) : void
        {
            var _loc_2:* = this.toolBar.searchKeyWord;
            var _loc_3:* = Math.min(this.viewPort.pageth, this.viewPort.pagesAllAvailable);
            if (_loc_2 && _loc_2 != "")
            {
                this.viewPort.doSearch(_loc_3, _loc_2);
            }
            return;
        }// end function

        protected function toolBarPagethChanged(event:Event = null) : void
        {
            this.viewPort.pageth = this.toolBar.pageth;
            return;
        }// end function

        protected function docViewSearchResult(event:Event = null) : void
        {
            this.toolBar.showSearchResult(this.viewPort.resultNum || 0);
            return;
        }// end function

        protected function toolBarSearchCloseBtnClick(event:Event = null) : void
        {
            this.viewPort.closeSearchResult();
            return;
        }// end function

    }
}
